﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Memo.Electricity.Engine
{
	public class CSingletonBehaviour<T> : MonoBehaviour
		where T : CSingletonBehaviour<T>
	{
		private static T s_Instance;
		private static bool b_IsQuit = false;
		public static T Inst
		{
			get
			{
				if (s_Instance == null && !b_IsQuit)
				{
					var root = GameObject.Find("Singletons");
					if (root == null)
					{
						root = new GameObject("Singletons");
						DontDestroyOnLoad(root);
					}
					s_Instance = new GameObject(typeof(T).Name).AddComponent<T>();
					s_Instance.transform.parent = root.transform;
				}
				return s_Instance;
			}
		}

		protected virtual void OnApplicationQuit()
		{
			b_IsQuit = true;
		}

	}

}

